package se19team1e.util;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Helper utility class that handles date from string and getting the string
 * from a date
 * 
 * @author arun
 * 
 */
public class DateUtils {
	/**
	 * In this static function, the string is formatted by the Formatter. THe
	 * format is set in the Constants file.
	 * 
	 * @param stringDate
	 *            String to be formatted
	 * @param format
	 *            the type of format the date is in.
	 * @return date in a Date class format
	 */
	public static Date getDateFromString(String stringDate, String format) {
		Date date = null;
		try {
			DateFormat formatter;
			formatter = new SimpleDateFormat(format);
			date = (Date) formatter.parse(stringDate);
		} catch (ParseException e) {
			System.out.println("Exception :" + e);
		}
		return date;
	}

	/**
	 * Static function that get the String from Date object
	 * 
	 * @param object
	 *            The Date object
	 * @param dateFormatString
	 *            format of the date to be used
	 * @return object that represents the date
	 */
	public static String getStringFromDate(Object object, String dateFormatString) {
		if (object==null){
			return Constants.EMPTY_STRING;
		}
		Date date = (Date) object;
		SimpleDateFormat dateFormat = new SimpleDateFormat(dateFormatString);
		return dateFormat.format(date);
	}

}
